package com.woniuxy.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {
    // 延迟队列（订单创建后发送到这里）
    public static final String ORDER_DELAY_QUEUE = "order.delay.queue";
    // 死信队列（订单超时后消息会进入这里）
    public static final String ORDER_DEAD_LETTER_QUEUE = "order.dead.letter.queue";
    // 延迟交换机
    public static final String ORDER_DELAY_EXCHANGE = "order.delay.exchange";
    // 死信交换机
    public static final String ORDER_DEAD_LETTER_EXCHANGE = "order.dead.letter.exchange";
    // 路由键
    public static final String ORDER_DELAY_ROUTING_KEY = "order.delay.routingKey";
    public static final String ORDER_DEAD_LETTER_ROUTING_KEY = "order.dead.letter.routingKey";
    // 支付成功消息队列
    public static final String ORDER_PAY_SUCCESS_QUEUE = "order.pay.success.queue";
    // 支付成功交换机
    public static final String ORDER_PAY_SUCCESS_EXCHANGE = "order.pay.success.exchange";
    // 支付成功路由键
    public static final String ORDER_PAY_SUCCESS_ROUTING_KEY = "order.pay.success";

    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }
    // 定义死信交换机
    @Bean
    DirectExchange orderDeadLetterExchange() {
        return new DirectExchange(ORDER_DEAD_LETTER_EXCHANGE);
    }

    // 定义死信队列
    @Bean
    Queue orderDeadLetterQueue() {
        return new Queue(ORDER_DEAD_LETTER_QUEUE);
    }

    // 绑定死信队列到死信交换机
    @Bean
    Binding bindingOrderDeadLetter() {
        return BindingBuilder.bind(orderDeadLetterQueue())
                .to(orderDeadLetterExchange())
                .with(ORDER_DEAD_LETTER_ROUTING_KEY);
    }

    // 定义延迟队列，并配置死信交换机和路由键
    @Bean
    Queue orderDelayQueue() {
        Map<String, Object> args = new HashMap<>();
        // 配置死信交换机
        args.put("x-dead-letter-exchange", ORDER_DEAD_LETTER_EXCHANGE);
        // 配置死信路由键
        args.put("x-dead-letter-routing-key", ORDER_DEAD_LETTER_ROUTING_KEY);
        return new Queue(ORDER_DELAY_QUEUE, true, false, false, args);
    }

    // 定义延迟交换机
    @Bean
    DirectExchange orderDelayExchange() {
        return new DirectExchange(ORDER_DELAY_EXCHANGE);
    }

    // 绑定延迟队列到延迟交换机
    @Bean
    Binding bindingOrderDelay() {
        return BindingBuilder.bind(orderDelayQueue())
                .to(orderDelayExchange())
                .with(ORDER_DELAY_ROUTING_KEY);
    }

    // 声明支付成功队列 - 修改为使用已定义的死信交换机
    @Bean
    public Queue orderPaySuccessQueue() {
        return QueueBuilder.durable(ORDER_PAY_SUCCESS_QUEUE)
                // 使用已定义的 ORDER_DEAD_LETTER_EXCHANGE
                .withArgument("x-dead-letter-exchange", ORDER_DEAD_LETTER_EXCHANGE)
                .withArgument("x-dead-letter-routing-key", ORDER_DEAD_LETTER_ROUTING_KEY)
                .withArgument("x-message-ttl", 600000) // 10分钟未处理进入死信
                .build();
    }

    // 声明支付成功交换机
    @Bean
    public DirectExchange orderPaySuccessExchange() {
        return new DirectExchange(ORDER_PAY_SUCCESS_EXCHANGE, true, false);
    }

    // 绑定队列和交换机
    @Bean
    public Binding bindPaySuccessQueue() {
        return BindingBuilder.bind(orderPaySuccessQueue())
                .to(orderPaySuccessExchange())
                .with(ORDER_PAY_SUCCESS_ROUTING_KEY);
    }

    // 添加JSON消息转换器（可选但推荐）
    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    // 配置消费者容器工厂（可选）
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(jsonMessageConverter());
        // 可配置其他属性，如并发消费者数量
        factory.setConcurrentConsumers(3);
        factory.setMaxConcurrentConsumers(10);
        return factory;
    }
}